<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12"></el-col>
      <el-col :span="12">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="loginFormRules"
          ref="loginFormRef"
        >
          <h1>hello</h1>
          <h2>欢迎来到vue-admin</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              type="primary"
              class="login_btn"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage, ElNotification } from 'element-plus'
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import userStoreFn from '@/store/modules/user'
import { getTimeSlot } from '@/utils/time'

const route = useRoute()
const router = useRouter()

const userStore = userStoreFn()

const loginForm = reactive({
  username: 'admin',
  password: '123456',
})

const loginFormRef = ref()
let loading = ref(false)

const validateUserName = (_rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('用户名格式错误'))
  }
}

const validatePassword = (_rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码格式错误'))
  }
}

const loginFormRules = reactive({
  //验证用户名合法性
  username: [
    {
      trigger: 'change',
      validator: validateUserName,
    },
  ],
  //验证密码合法性
  password: [
    {
      trigger: 'change',
      validator: validatePassword,
    },
  ],
})

const login = () => {
  loginFormRef.value.validate(async (valid: boolean) => {
    if (!valid) {
      ElMessage({
        type: 'error',
        message: '请检查表单是否填写正确',
      })
      return
    }
    loading.value = true
    const loginResult = userStore.login(loginForm)
    loginResult
      .then(
        (): void => {
          router.push((route.query.redirect as string) || '/')
          ElNotification({
            type: 'success',
            message: '登录成功',
            duration: 2000,
            title: `HI ${getTimeSlot()}好`,
          })
        },
        (error: Error): void => {
          ElNotification({
            type: 'error',
            message: error.message,
            duration: 2000,
          })
        },
      )
      .finally(() => {
        loading.value = false
      })
  })
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url(@/assets/images/background.jpg) no-repeat;
  background-size: cover;
}

.login_form {
  width: 80%;
  top: 30vh;
  position: relative;
  background: url(@/assets/images/login_form.png) no-repeat;
  background-size: cover;
  color: white;
  padding: 40px;

  h1 {
    font-size: 40px;
  }
  h2 {
    font-size: 20px;
    margin: 20px 0;
  }

  .login_btn {
    width: 100%;
  }
}
</style>
